#include<iostream>

#include<unistd.h>

#include <fcntl.h>

#include<errno.h>

#include<string.h>

using namespace std;

void SetNoBlock(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if(fl < 0)
    {
        perror("fcntl");
        return; 
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}

int main()
{
    char buffer[1024] = {0};
    SetNoBlock(0);
    while(1)
    {
        printf("please enter# ");
        fflush(stdout);
        int n = read(0, (void*)buffer, sizeof(buffer));
        if(n > 0)
        {
            buffer[n] = 0;
            printf("%s", buffer);
        }
        else if(n < 0)
        {
            if(errno == 11)
                cout << "errno: " << errno << ", error reason: " << strerror(errno) << endl;
            else
            {
                printf("serious error, exit");
                break;
            }
        }
        else
        {
            printf("end of stdin, close fd and exit\n");
            close(0);
            break;
        }
        sleep(1);
    }
    return 0;
}